package com.whmskj.xjlhbc.mobile.schedule;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whmskj.xjlhbc.domain.Bchb01zjrhb;
import com.whmskj.xjlhbc.domain.Bchb02jsrhbzb;
import com.whmskj.xjlhbc.domain.Bchb06cpcmrjdb;
import com.whmskj.xjlhbc.domain.ScProductionOrders;
import com.whmskj.xjlhbc.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * 成品处每日生产进度处理
 */
@Slf4j
@Component
public class Bchb06cpcmrjdbTask {

    //成品处每日生产进度
    @Autowired
    private Bchb06cpcmrjdbMapper bchb06cpcmrjdbMapper;

    //浆纱
    @Autowired
    private Bchb02jsrhbzbMapper bchb02jsrhbzbMapper;

    //整经
    @Autowired
    private Bchb01zjrhbMapper bchb01zjrhbMapper;

    //订单
    @Autowired
    private ScProductionOrdersMapper scProductionOrdersMapper;

    //查品种开台
    @Autowired
    private Sb11dknittingstationstiedMapper sb11dknittingstationstiedMapper;


    @Scheduled(cron = "0 0/10 * * * ?")
    public void update(){
        LambdaQueryWrapper<ScProductionOrders> scProductionOrdersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        scProductionOrdersLambdaQueryWrapper.eq(ScProductionOrders::getOrderStatus, "生产中")
                .orderByAsc(ScProductionOrders::getNextDay).last("limit 1");
        ScProductionOrders scProductionOrders = scProductionOrdersMapper.selectOne(scProductionOrdersLambdaQueryWrapper);
        if(Objects.isNull(scProductionOrders))return;
        String noNo3 = scProductionOrders.getNoNo3();//订单品种
        String today = DateUtil.today();
        LambdaQueryWrapper<Bchb06cpcmrjdb> bchb06cpcmrjdbLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bchb06cpcmrjdbLambdaQueryWrapper.eq(Bchb06cpcmrjdb::getPhnum,noNo3).eq(Bchb06cpcmrjdb::getTjdate,today);
        List<Bchb06cpcmrjdb> bchb06cpcmrjdbs = bchb06cpcmrjdbMapper.selectList(bchb06cpcmrjdbLambdaQueryWrapper);
        long number = NumberUtil.parseLong(scProductionOrders.getNumber(), 0L);
        long clSum = bchb06cpcmrjdbs.stream().mapToLong(Bchb06cpcmrjdb::getMbdrcl).sum();//码布

        //浆纱
        LambdaQueryWrapper<Bchb02jsrhbzb> bchb02jsrhbzbLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bchb02jsrhbzbLambdaQueryWrapper.eq(Bchb02jsrhbzb::getPhnum,noNo3).eq(Bchb02jsrhbzb::getTjdate,today);
        List<Bchb02jsrhbzb> bchb02jsrhbs = bchb02jsrhbzbMapper.selectList(bchb02jsrhbzbLambdaQueryWrapper);
        long jsSum = bchb02jsrhbs.stream().mapToLong(m->NumberUtil.parseLong(m.getClength(),0L)).sum();//浆纱

        //整经
        LambdaQueryWrapper<Bchb01zjrhb> bchb01zjrhbLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bchb01zjrhbLambdaQueryWrapper.eq(Bchb01zjrhb::getPhnum,noNo3).eq(Bchb01zjrhb::getTjdate,today);
        List<Bchb01zjrhb> bchb01zjrhbs = bchb01zjrhbMapper.selectList(bchb01zjrhbLambdaQueryWrapper);
        long zjSum = bchb01zjrhbs.stream().mapToLong(m->NumberUtil.parseLong(m.getClength(),0L)).sum();//浆纱

        Map<String, Object> defaultMap = new HashMap<>();
        defaultMap.put("numberOfOpenings1", BigDecimal.valueOf(0L));
        defaultMap.put("numberOfOpenings2", BigDecimal.valueOf(0L));
        Map<String, Object> map = Optional.ofNullable(sb11dknittingstationstiedMapper.selectPzktDetail(noNo3)).orElse(defaultMap);

        ScProductionOrders updateOrder = new ScProductionOrders();
        updateOrder.setId(scProductionOrders.getId());
        updateOrder.setDmbcl(Math.min(number,clSum));
        updateOrder.setDjscl(jsSum);
        updateOrder.setDzjcl(zjSum);
        updateOrder.setKt190(((BigDecimal) map.get("numberOfOpenings1")).longValue());
        updateOrder.setKt230(((BigDecimal) map.get("numberOfOpenings2")).longValue());
        scProductionOrdersMapper.updateById(updateOrder);
    }
}
